package com.catherine.mvp_base_component.realm;

import com.catherine.mvp_base_component.app.BaseApplication;
import com.catherine.mvp_base_component.utils.LogUtil;

import java.util.HashSet;
import java.util.Set;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmMigration;
import io.realm.RealmSchema;

public class RealmKeyUtils {
    private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
    static RealmConfiguration realmConfig = null;

    public static void setInitWalletOCRealm() {
        //此处根据业务调用初始化，避免出现默认库bug
        if (Realm.getDefaultConfiguration() == null)
            Realm.init(BaseApplication.getAppContext());
        if (!Realm.getDefaultConfiguration().getRealmFileName().equals("wallet_dc")) {
            LogUtil.d("=============realmDBName  !equals======setWalletOCRealm=====");
            setWalletOCRealm();
        }
    }

    private static void setWalletOCRealm() {
//        ECKey ecKey = ECKey.fromPrivate(OcMath.toBigInt("0abc4301"));
//        byte[] sha256 = Sha256Hash.hash(ecKey.getPubKey());
//           //解密
//        LogUtil.d("sha256---------"+OcMath.toHexStringNoPrefix(OcMath.toHexStringNoPrefix(sha256).getBytes()));
        realmConfig = new RealmConfiguration.Builder()
                //.schemaVersion(ConfigRealm.DB_VER)
//                .migration(migration_wallet_dc)
                //设置数据库密码
//                .encryptionKey(OcMath.toHexStringNoPrefix(sha256).getBytes())
                //.deleteRealmIfMigrationNeeded()
                .name("wallet_dc.realm")
                .schemaVersion(version_wallet_dc)
                .build();
        mConfigsList.add(realmConfig);
        Realm.setDefaultConfiguration(realmConfig);
    }

    public static Realm getRealm() {
        return Realm.getInstance(getRealmConfiguration());
    }

    public static RealmConfiguration getRealmConfiguration() {
        if (realmConfig == null) {
            setWalletOCRealm();
        }
        return realmConfig;
    }

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    protected static Set<RealmConfiguration> mConfigsList = new HashSet<RealmConfiguration>();


    private static final int version_wallet_dc = 1;  // 数据库版本号

    /**
     * 升级wallet_oc数据库
     */
    protected static RealmMigration migration_wallet_dc = (realm, oldVersion, newVersion) -> {
        RealmSchema schema = realm.getSchema();
        //数据库升级
/*        if (oldVersion == 1) {
            schema.get("Rm_WalletDC")
                    .addField("deletedStatus", String.class)
                    .addField("isChecked", boolean.class)
                    .addField("others", String.class);
            oldVersion++;
        }*/
    };
}
